package com.donggua.aichebao.commonui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 垂直三个TextView
 * Created by fzh on 2017/8/22.
 */

public class ThreeTextViewLayout extends LinearLayout {
    private TextView tvFirst;
    private TextView tvSecond;
    private TextView tvThird;
    private static final int DEFAULT_SIZE = 40;
    private static final int DEFAULT_COLOR = 0xff000000;
    private float density = 1;
    public ThreeTextViewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        density = context.getResources().getDisplayMetrics().density;
        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.CENTER_HORIZONTAL);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomThreeTextView);
        String strFirst = ta.getString(R.styleable.CustomThreeTextView_customFirstText);
        int firstSize = (int)ta.getDimension(R.styleable.CustomThreeTextView_customFirstTextSize,DEFAULT_SIZE);
        int firstColor = ta.getColor(R.styleable.CustomThreeTextView_customFirstTextColor,DEFAULT_COLOR);
        String strSecond = ta.getString(R.styleable.CustomThreeTextView_customSecondText);
        int secondSize = (int)ta.getDimension(R.styleable.CustomThreeTextView_customSecondTextSize,DEFAULT_SIZE);
        int secondColor = ta.getColor(R.styleable.CustomThreeTextView_customSecondTextColor,DEFAULT_COLOR);
        String strThree = ta.getString(R.styleable.CustomThreeTextView_customThreeText);
        int thirdSize = (int)ta.getDimension(R.styleable.CustomThreeTextView_customThreeTextSize,DEFAULT_SIZE);
        int thirdColor = ta.getColor(R.styleable.CustomThreeTextView_customThreeTextColor,DEFAULT_COLOR);

        LinearLayout.LayoutParams layoutParams = getMyLayoutParams();
        layoutParams.topMargin = dpToPx(5);
        tvFirst = getTextView();
        tvFirst.setLayoutParams(layoutParams);
        tvFirst.setText(strFirst);
        tvFirst.setTextColor(firstColor);
        tvFirst.setTextSize(TypedValue.COMPLEX_UNIT_PX,firstSize);
        this.addView(tvFirst);
        tvSecond = getTextView();
        tvSecond.setLayoutParams(layoutParams);
        tvSecond.setText(strSecond);
        tvSecond.setTextColor(secondColor);
        tvSecond.setTextSize(TypedValue.COMPLEX_UNIT_PX,secondSize);
        this.addView(tvSecond);
        tvThird = getTextView();
        tvThird.setLayoutParams(layoutParams);
        tvThird.setText(strFirst);
        tvThird.setTextColor(firstColor);
        tvThird.setTextSize(TypedValue.COMPLEX_UNIT_PX,firstSize);
        this.addView(tvThird);
        ta.recycle();
    }

    public int dpToPx(int dp){
        int px = (int)(dp*density+0.5);
        return px;
    }
    private LinearLayout.LayoutParams getMyLayoutParams(){
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }

    private TextView getTextView(){
        TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    public void setFistText(String text){
        tvFirst.setText(text);
    }

    public void setSecondText(String text){
        tvSecond.setText(text);
    }

    public void setThirdText(String text){
        tvThird.setText(text);
    }
}
